package com.rc.saas.mini.service.ugs;

import com.rc.saas.mini.common.constant.WebConstant;
import com.rc.saas.mini.mapper.ugs.TenantUgsLevelTypeMapper;
import com.rc.saas.mini.mapper.ugs.TenantUgsMemberLevelMapper;
import com.rc.saas.mini.mapper.ugs.UgsMemberLevelRecordMapper;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.ugs.*;
import com.rc.saas.mini.vo.ugs.TenantUgsLevelTypeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TenantUgsMemberLevelServiceImpl implements TenantUgsMemberLevelService {

    @Autowired
    private TenantUgsMemberLevelMapper tenantUgsMemberLevelMapper;

    @Autowired
    private TenantUgsLevelTypeService tenantUgsLevelTypeService;

    @Autowired
    private UgsMemberLevelRecordMapper ugsMemberLevelRecordMapper;

    @Autowired
    private TenantUgsLevelTypeMapper tenantUgsLevelTypeMapper;

    @Override
    public TenantUgsMemberLevelMapper getMapper() {
        return tenantUgsMemberLevelMapper;
    }

    @Override
    public void insertTenantUgsMemberLevel(MemberInfo memberInfo) {
        TenantUgsMemberLevel tenantUgsMemberLevel = tenantUgsMemberLevelMapper.selectByPrimaryKey(memberInfo.getMemberId());
        //不存在用户等级数据
        if (tenantUgsMemberLevel == null) {
            TenantUgsLevelTypeExample example = new TenantUgsLevelTypeExample();
            example.createCriteria().andLevelTypeEqualTo("L1").andTenantCodeEqualTo(memberInfo.getTenantCode()).andStatusEqualTo(WebConstant.CON0);
            TenantUgsLevelType tenantUgsLevelType = tenantUgsLevelTypeService.selectByExample(example).stream().findFirst().orElseGet(() -> null);
            if (tenantUgsLevelType != null) {
                tenantUgsMemberLevel = new TenantUgsMemberLevel();
                tenantUgsMemberLevel.setMemberId(memberInfo.getMemberId());
                tenantUgsMemberLevel.setLevelType(tenantUgsLevelType.getLevelType());
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE, tenantUgsLevelType.getExpireDay());
                tenantUgsMemberLevel.setTotoalExpValue(0);
                tenantUgsMemberLevel.setKeepTime(calendar.getTime());
                tenantUgsMemberLevel.setTenantCode(memberInfo.getTenantCode());

                tenantUgsMemberLevel.setShareNum(0);
                tenantUgsMemberLevel.setTotoalExpValue(0);
                tenantUgsMemberLevel.setCarAuthType(WebConstant.CON0);
                tenantUgsMemberLevel.setSigninDay(0);
                tenantUgsMemberLevel.setSigninType(WebConstant.CON0);
                tenantUgsMemberLevel.setRealnameAuthType(WebConstant.CON0);
                tenantUgsMemberLevel.setCreatedBy(String.valueOf(memberInfo.getMemberId()));
                tenantUgsMemberLevel.setCreatedTime(new Date());
                tenantUgsMemberLevelMapper.insertSelective(tenantUgsMemberLevel);
            }

        }
    }

    @Override
    public void updateReduceLevel(Long memberId) {
        //用户当前等级
        TenantUgsMemberLevel tenantUgsMemberLevel = tenantUgsMemberLevelMapper.selectByPrimaryKey(memberId);
        if (tenantUgsMemberLevel != null) {
            //是否已到保值期
            if (tenantUgsMemberLevel.getKeepTime().getTime() < System.currentTimeMillis()) {
                UgsMemberLevelRecord ugsMemberLevelRecord = new UgsMemberLevelRecord();
                //当前级别设置
                TenantUgsLevelType tenantUgsLevelType = tenantUgsLevelTypeService.selectByLevelType(tenantUgsMemberLevel.getTenantCode(), tenantUgsMemberLevel.getLevelType());
                //记录当前等级  成长值  有效时间
                ugsMemberLevelRecord.setBeforeValue(tenantUgsMemberLevel.getTotoalExpValue());
                ugsMemberLevelRecord.setBeforeLevelType(tenantUgsMemberLevel.getLevelType());
                ugsMemberLevelRecord.setBeforeTime(tenantUgsMemberLevel.getKeepTime());
                Calendar calendar = Calendar.getInstance();
                Date keepTime = null;

                if ("L1".equals(tenantUgsMemberLevel.getLevelType())) {
                    calendar.add(Calendar.DATE, tenantUgsLevelType.getExpireDay());
                    keepTime = calendar.getTime();
                    tenantUgsMemberLevel.setTotoalExpValue(0);
                    tenantUgsMemberLevel.setKeepTime(keepTime);
                } else {
                    if (tenantUgsLevelType.getKeepValue().compareTo(tenantUgsMemberLevel.getTotoalExpValue()) <= 0) {
                        //1.降值不降级
                        calendar.add(Calendar.DATE, tenantUgsLevelType.getExpireDay());
                        keepTime = calendar.getTime();
                        tenantUgsMemberLevel.setTotoalExpValue(tenantUgsLevelType.getMinValue());
                        tenantUgsMemberLevel.setKeepTime(keepTime);

                    } else {
                        //2.降级且降值
                        //前一级别
                        String level = tenantUgsLevelType.getLevelType();
                        Integer num = Integer.valueOf(StringUtils.substring(level, -1));
                        num = num - 1;
                        String beforeLevel = StringUtils.substring(level, 0, 1) + num;
                        //前一级别设置
                        TenantUgsLevelType beforeTenantUgsLevelType = tenantUgsLevelTypeService.selectByLevelType(tenantUgsMemberLevel.getTenantCode(), tenantUgsMemberLevel.getLevelType());
                        calendar.add(Calendar.DATE, beforeTenantUgsLevelType.getExpireDay());
                        keepTime = calendar.getTime();

                        tenantUgsMemberLevel.setTotoalExpValue(beforeTenantUgsLevelType.getKeepValue());
                        tenantUgsMemberLevel.setLevelType(beforeLevel);
                        tenantUgsMemberLevel.setKeepTime(keepTime);

                    }
                }

                tenantUgsMemberLevel.setLastUpdatedBy(String.valueOf(memberId));
                tenantUgsMemberLevel.setLastUpdatedTime(new Date());

                tenantUgsMemberLevelMapper.updateByPrimaryKeySelective(tenantUgsMemberLevel);
                //更新ugs_member_level_record

                ugsMemberLevelRecord.setTenantCode(tenantUgsMemberLevel.getTenantCode());
                ugsMemberLevelRecord.setMemberId(memberId);
                ugsMemberLevelRecord.setRecordType("0");
                ugsMemberLevelRecord.setGrowingTypeId(0L);
                ugsMemberLevelRecord.setAfterLevelType(tenantUgsMemberLevel.getLevelType());
                ugsMemberLevelRecord.setAfterValue(tenantUgsMemberLevel.getTotoalExpValue());
                ugsMemberLevelRecord.setAfterTime(tenantUgsMemberLevel.getKeepTime());
                ugsMemberLevelRecord.setRemark("到达有效期降级处理");
                ugsMemberLevelRecord.setCreatedTime(new Date());
                ugsMemberLevelRecord.setLastUpdatedTime(new Date());
                ugsMemberLevelRecordMapper.insertSelective(ugsMemberLevelRecord);
            }
        }
    }

    @Override
    public TenantUgsMemberLevel selectByMemberId(String tenantCode, Long memberId) {
        TenantUgsMemberLevelExample example = new TenantUgsMemberLevelExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andTenantCodeEqualTo(tenantCode);
        return tenantUgsMemberLevelMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public TenantUgsMemberLevel addTenantUgsMemberLevel(Long memberId, String tenantCode) {

        List<TenantUgsLevelType> tenantUgsLevelTypes = tenantUgsLevelTypeMapper.selectByTenantCode(tenantCode);
        TenantUgsLevelType tenantUgsLevelType1 =
                tenantUgsLevelTypes.stream().filter(tenantUgsLevelType -> "L1".equals(tenantUgsLevelType.getLevelType())).findFirst().orElse(null);
        TenantUgsMemberLevel tenantUgsMemberLevel = new TenantUgsMemberLevel();
        tenantUgsMemberLevel.setMemberId(memberId);
        tenantUgsMemberLevel.setTenantCode(tenantCode);
        tenantUgsMemberLevel.setLevelType("L1");
        Date today;
        //SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, tenantUgsLevelType1.getExpireDay());
        today = calendar.getTime();
        tenantUgsMemberLevel.setKeepTime(today);
        tenantUgsMemberLevel.setCreatedTime(new Date());
        tenantUgsMemberLevel.setLastUpdatedTime(new Date());
        tenantUgsMemberLevelMapper.insertSelective(tenantUgsMemberLevel);
        return tenantUgsMemberLevel;
    }
}

